﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.IO;
using System.Web.Security;
using System.Timers;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web.Mail;

/// <summary>
/// Summary description for Dao
/// </summary>
public class Dao
{
    
	public Dao()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static DataSet Get_Thongtin(int index,int num,string sql)
    {



        string kn;
        string aa="SB001";

        
        //Da.Fill(Ds, index, NUM_ROWS_PER_PAGE, "SB001");
       

        //SqlCommand cm = new SqlCommand();
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlDataAdapter db = new SqlDataAdapter("SELECT * from SB001 order by ID desc ", cn);
        DataSet ds = new DataSet();
        db.Fill(ds,index,num,aa);
        cn.Close();
        return ds;
    }

    public static DataSet Get_Logo(string logo)
    {



        string kn;
        //SqlCommand cm = new SqlCommand();
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlDataAdapter db = new SqlDataAdapter("SELECT top 4* FROM Logo where Loai='lo'order by ID desc ", cn);
        DataSet ds = new DataSet();
        db.Fill(ds);

        cn.Close();
        return ds;
    }
    public static int Get_Thang_Het_Hang_Visa(string code,string sql)
    {
        string kn;
        int count;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("SELECT DATEPART(month,(select ExpiredDate from VisaCard where Code=@code))", cn);
      //  SqlCommand cmd = new SqlCommand(sql, cn);
        
        cmd.Parameters.Add("@code", code);
        count = (int)cmd.ExecuteScalar();
        
        return count;
    }
   
    public static int Get_Nam_Het_Hang_Visa(string code,string sql)
    {
        string kn;
        int count;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("SELECT DATEPART(year,(select ExpiredDate from VisaCard where Code=@code))", cn);
                
        cmd.Parameters.Add("@code", code);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static int Get_Thang_Het_Hang_Master(string code, string sql)
    {
        string kn;
        int count;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("SELECT DATEPART(month,(select ExpiredDate from MasterCard where Code=@code))", cn);
        //  SqlCommand cmd = new SqlCommand(sql, cn);

        cmd.Parameters.Add("@code", code);
        count = (int)cmd.ExecuteScalar();

        return count;
    }

    public static int Get_Nam_Het_Hang_Master(string code, string sql)
    {
        string kn;
        int count;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("SELECT DATEPART(year,(select ExpiredDate from MasterCard where Code=@code))", cn);

        cmd.Parameters.Add("@code", code);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
   
   
    public static int Nam_het_han_taikhoan(int nm, int th,string code)
    {
        string kn;
        int count;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from VisaCard where (SELECT DATEPART(month,(select ExpiredDate from VisaCard where Code=@code))=@th) and (SELECT DATEPART(year,(select ExpiredDate from VisaCard where Code=@code))=@nm)", cn);
        cmd.Parameters.Add("@nm", nm);
        cmd.Parameters.Add("@th", th);
        cmd.Parameters.Add("@code",code); 
        count = (int)cmd.ExecuteScalar();
        return count;        
    }
    public static int Kiemtra_Taikhoan_Visa(string code)
    {
        string kn;
        string sql_;
        int count;
        string chuoi;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
       // chuoi="where Code=@code";
        sql_ = "select Count(*) from VisaCard where Code=@code";
       //// sql_+=sql+"where Code=@code";
        
        SqlCommand cmd = new SqlCommand(sql_, cn);
        cmd.Parameters.Add("@code", code);        
        count = (int)cmd.ExecuteScalar();
        return count;  
    
    }
    public static int Kiemtra_Taikhoan_Master(string code)
    {
        string kn;
        string sql_;
        int count;
        string chuoi;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        // chuoi="where Code=@code";
        sql_ = "select Count(*) from MasterCard where Code=@code";
        //// sql_+=sql+"where Code=@code";

        SqlCommand cmd = new SqlCommand(sql_, cn);
        cmd.Parameters.Add("@code", code);
        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static int Select_try(string ip, string day)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Try from Code where IP=@ip and Day_time=@day", cn);
        cmd.Parameters.Add("@ip", ip);
        cmd.Parameters.Add("@day",day);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static string Select_day(string ip, int view)
    {
        string count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Day_time from code where IP=@ip", cn);
        cmd.Parameters.Add("@ip", ip);
        //cmd.Parameters.Add("@view",view);
        count = (string)cmd.ExecuteScalar();
        return count;
    }


    public static int Login(string user, string pass,string act)
    {
        int count;
        string kn;

        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
         
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();



        SqlCommand cmd = new SqlCommand("Select Count(*) from KH001 where TenDangNhap=@username and [MatKhau]=@password and TinhTrang=@act", cn);
        cmd.Parameters.Add("@username", user);
        cmd.Parameters.Add("@password", pass);
        cmd.Parameters.Add("@act", act);
        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static long nam_thang_ngay_gio_phut(DateTime dt, int nm, int th, int ng, int gio, int phut)
    {
        long tong=0;
        if (dt.Year == nm)
        {
            //tong = th - dt.Month;
            if (th - dt.Month == 0)
            {
                if (ng - dt.Day == 0)
                {
                    if (gio - dt.Hour == 0)
                        tong = phut - dt.Minute;
                    else
                        tong = (gio - dt.Hour) * 60 + (phut - dt.Minute);
                }
                else
                {
                    tong = ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour) * 60 + phut - dt.Minute;
                }
            }
            else
            {
                if (th == 1 || th == 3 || th == 5 || th == 7 || th == 8 || th == 10 || th == 12)
                {
                    tong = (th - dt.Month) * 31 * 24 * 60 + ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour * 60) + (phut - dt.Minute);
                }
                else if (th == 4 || th == 6 || th == 9 || th == 11)
                    tong = (th - dt.Month) * 30 * 24 * 60 + ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour * 60) + (phut - dt.Minute);
                else
                    tong = (th - dt.Month) * 28 * 24 * 60 + ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour * 60) + (phut - dt.Minute);
            }

        }
        else
        { 
            if (th == 1 || th == 3 || th == 5 || th == 7 || th == 8 || th == 10 || th == 12)
                tong = ((nm - dt.Year) * 365 * 24 * 60) + (th - dt.Month) * 31 * 24 * 60 + ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour * 60) + (phut - dt.Minute);
            if (th == 4 || th == 6 || th == 9 || th == 11)
                tong = ((nm - dt.Year) * 365 * 24 * 60) + (th - dt.Month) * 30 * 24 * 60 + ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour * 60) + (phut - dt.Minute);
            if(th==2)
                tong = ((nm - dt.Year) * 365 * 24 * 60) + (th - dt.Month) * 28 * 24 * 60 + ((ng - dt.Day) * 24 * 60) + (gio - dt.Hour * 60) + (phut - dt.Minute);
        }


            
        
        return tong;

    }
    public static int Them_danhmuc(string ten)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ConnectionString;
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        //connection();


        SqlCommand cmd = new SqlCommand("Select Count(*) from DM001 where DienGiai=@ten", cn);
        cmd.Parameters.Add("@ten", ten);        
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static int Them_danh_muc_sp(string ten)
    {
        string kn;
        string sql;
        string up;
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        sql = "insert into DM001(DienGiai) values(@ten)";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@ten",ten);
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static int Them_sp(string ten,int id)
    {
        string kn;
        string sql;
        string up;
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        sql = "insert into DM002(DienGiai,ID_DM001) values(@ten,@id)";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@ten", ten);
        cm.Parameters.Add("@id", id);
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static int Kiemtra_sp(string ten)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ConnectionString;
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();   


        SqlCommand cmd = new SqlCommand("Select Count(*) from DM002 where DienGiai=@ten", cn);
        cmd.Parameters.Add("@ten", ten);
        count = (int)cmd.ExecuteScalar();
        return count;
    }

    //hình thức thanh toán
    public static int Kiemtra_hinhthucthanhtoan(string ten)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ConnectionString;
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from TT001 where HTTT=@ten", cn);
        cmd.Parameters.Add("@ten", ten);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static int Them_moi(string ten)
    {
        string kn;
        string sql;
        string up;
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        sql = "insert into TT001(HTTT) values(@ten)";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@ten", ten);        
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static int TestRegister(string user, string email)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ConnectionString;
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        //connection();


        SqlCommand cmd = new SqlCommand("Select Count(*) from KH001 where TenDangNhap=@username or [Email]=@email", cn);
        cmd.Parameters.Add("@username", user);
        cmd.Parameters.Add("@email", email);        
        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static DataSet danh_muc_sp()
    {

        string kn;

        //SqlCommand cm = new SqlCommand();
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlDataAdapter db = new SqlDataAdapter("select * from DM001 order by DienGiai", cn);
        DataSet ds = new DataSet();        
        db.Fill(ds);
        cn.Close();
        return ds;
    }
    public static int Select(string ip, string day)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from code where IP=@IP and [Day_time]=@DAY", cn);
        cmd.Parameters.Add("@IP", ip);
        cmd.Parameters.Add("@DAY", day);
        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static int Compare_Code(string code,string ip,string ngay)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from code where Code=@code and IP=@ip and Day_time=@ngay", cn);
        cmd.Parameters.Add("@code", code);
        cmd.Parameters.Add("@ip",ip);
        cmd.Parameters.Add("@ngay",ngay);

        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static string Compare(string ip, string ngay)
    {
        string count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Code from code where IP=@ip and Day_time=@ngay", cn);
      
        cmd.Parameters.Add("@ip", ip);
        cmd.Parameters.Add("@ngay", ngay);

        count = (string)cmd.ExecuteScalar();
        return count;

    }

    public static int Kich_hoat(string act,string tt,string us)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Update KH001 set TinhTrang=@tt where Code=@act and TenDangNhap=@us", cn);
        cmd.Parameters.Add("@act", act);
        cmd.Parameters.Add("@tt",tt);
        cmd.Parameters.Add("@us", us);
        count = (int)cmd.ExecuteNonQuery();
        return count;

    }
    public static int Kich_hoat_continue(string act, string tt, string us)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select Count(*) from KH001 where TinhTrang=@tt and Code=@act and TenDangNhap=@us", cn);
        cmd.Parameters.Add("@act", act);
        cmd.Parameters.Add("@tt", tt);
        cmd.Parameters.Add("@us", us);
        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static int Select_Act(string act)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select ID from KH001 where Code=@act", cn);        
        cmd.Parameters.Add("@act", act);
        count = (int)cmd.ExecuteScalar();
        return count;

    }
    public static int SelectCode(int trycode)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from code where Try>=@try", cn);
        cmd.Parameters.Add("@try", trycode);
        count = (int)cmd.ExecuteScalar();
        return count;

    }
 
    public static int testcode(string stcode,string ip)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from code where Code=@code and IP=@ip", cn);
        cmd.Parameters.Add("@code", stcode);
        cmd.Parameters.Add("@ip",ip);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static int Get_user(string us)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select ID from KH001 where TenDangNhap=@us", cn);        
        cmd.Parameters.Add("@us", us);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static int Test_fogot(string email)
    {
        int count;
        string kn;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select Count(*) from KH001 where Email=@mail", cn);

        cmd.Parameters.Add("@mail",email);
        count = (int)cmd.ExecuteScalar();
        return count;
    }
    public static bool CheckRegister(string us, string pa, string name,string ngaysinh,string cmnd,string diachi, string mail,string thang,string nam,string mathe, string code)
    {
        bool flag = true;
        if (us == "" || pa == "" || name == ""||ngaysinh==""||cmnd==""||diachi==""||mail == ""||thang==""||nam==""||mathe=="" || code == "")
            flag = false;
        return flag;
    }
    public static int ChangePassword(string p,int id)
    {
        string kn;               
        int rs;
        string sql;
       
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        //sql = "update code set Try=Try+1,Code=@code where Ip=@ip and Day_time=@Daycurent";
        sql = "update KH001 set MatKhau=@p where ID=@id and TinhTrang='u'";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@p", p);
        cm.Parameters.Add("@id", id);
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static DataSet Change_thongtin(int id)
    {

        string kn;

        //SqlCommand cm = new SqlCommand();
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlDataAdapter db = new SqlDataAdapter("select * from KH001 where ID=id", cn);        
        DataSet ds = new DataSet();
        db.Fill(ds);
        cn.Close();
        return ds;
    }

    public static int Update(string ip, string code)
    {
        string kn;
        string sql;
        string up;
        int rs;
        int num = 10;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        //sql = "update code set Try=Try+1,Code=@code where Ip=@ip and Day_time=@Daycurent";
        sql = "update code set Try=Try+1,Code=@code";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@ip", ip);
        cm.Parameters.Add("@code", code);
        //cm.Parameters.Add("@Number", num);
        //cm.Parameters.Add("@Daycurent", day);
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;

    }
    public static int InsertCode(string Ip, string Code, int Num, string Day)
    {
        //SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["db_connection"]);
        string kn;
        string sql;
        string up;
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        sql = "insert into code(IP,Code,Try,Day_time) values(@ip,@code,@Number,@Daycurent)";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@ip", Ip);
        cm.Parameters.Add("@code", Code);
        cm.Parameters.Add("@Number", Num);
        cm.Parameters.Add("@Daycurent", Day);
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static int InsertBaner_Logo(string ten, string loai,string link, string ngay)
    {
        //SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["db_connection"]);
        string kn;
        string sql;
      
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        sql = "insert into Logo(Logo,Loai,Link,Ngay) values(@logo,@loai,@link,@ngay)";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@logo", ten);
        cm.Parameters.Add("@loai", loai);
        cm.Parameters.Add("@link", link);
        cm.Parameters.Add("@ngay", ngay);        
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static int Register(string us, string pas, string tinhtrang, string fulname, DateTime ngaysinh,string cmnd,string diachi, string email, string code)
    {
        //SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["db_connection"]);
        string kn;
        string sql;
        
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        
        //sql = "insert into KH001(TenDangNhap,MatKhau,TinhTrang,HoTen,NTNS,CMND,DiaChi,Email,Code) values(@us,@pas,@tinhtrang,@name,@ngaysinh,@cmnd,@diachi,@email,@code)";
        sql = "insert into KH001(TenDangNhap,MatKhau,TinhTrang,HoTen,NTNS,CMND,DiaChi,Email,Code) values(@us,@pas,@tinhtrang,@name,@ngaysinh,@cmnd,@diachi,@email,@code)";
        SqlCommand cm = new SqlCommand(sql, cn);        
        cm.Parameters.Add("@us",us);
        cm.Parameters.Add("@pas",pas);
        cm.Parameters.Add("@tinhtrang",tinhtrang);
        cm.Parameters.Add("@name",fulname);
        cm.Parameters.Add("@ngaysinh",ngaysinh);
        cm.Parameters.Add("@cmnd",cmnd);
        cm.Parameters.Add("@diachi",diachi);
        cm.Parameters.Add("@email",email);
        cm.Parameters.Add("@code",code);
        //cm.Parameters.Add("@act",act);        
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static int Register_Continue(int id,string ho,string ma, string loaithe,string mathe,string th,string nm)
    {
        //SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["db_connection"]);
        string kn;
        string sql;

        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
       
        sql = "insert into KH002(ID_01,HoTen,Ma,LoaiThe,MaThe,Thang,Nam) values(@id,@ho,@ma,@loai,@mathe,@th,@nm)";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add("@id", id);
        cm.Parameters.Add("@ho", ho);
        cm.Parameters.Add("@ma", ma);
        cm.Parameters.Add("@loai",loaithe);
        cm.Parameters.Add("@mathe",mathe);
        cm.Parameters.Add("@th",th);
        cm.Parameters.Add("@nm",nm);        
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;
    }
    public static void Sentmail(string from, string to, string body, string subject, string severmail)
    {
        MailMessage mail = new MailMessage();
        mail.From = from;
        mail.To = to;
        mail.Subject = subject;
        mail.Body = body.ToString();
        SmtpMail.SmtpServer = severmail; 
        SmtpMail.Send(mail);

    }



    public static int delete(string ip)
    {

        string kn;
        int rs;
        kn = ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection cn = new SqlConnection(kn);
        cn.Open();
        SqlCommand cm = new SqlCommand("delete from code where IP=@ip ", cn);
        cm.Parameters.Add("@ip",ip);   
        rs = (int)cm.ExecuteNonQuery();
        cn.Close();
        return rs;


    }


    public static string getMd5Hash(string input)
    {
        MD5 md5Hasher = MD5.Create();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }
    public static bool verifyMd5Hash(string input, string hash)
    {
        // Hash the input.
        string hashOfInput = getMd5Hash(input);

        // Create a StringComparer an comare the hashes.
        StringComparer comparer = StringComparer.OrdinalIgnoreCase;

        if (0 == comparer.Compare(hashOfInput, hash))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static string RandomString()
    {

        int size = 6;

        Random r = new Random();

        string legalChars = "A0BC5DEFG9HJ8KL4MNP7QRS1TW2XY3Z6";

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < size; i++)

            sb.Append(legalChars.Substring(r.Next(0, legalChars.Length - 1), 1));

        return sb.ToString();

    }
    public static string RandomString_Active()
    {

        int size = 15;

        Random r = new Random();

        string legalChars = "A59BC3DEFzG0yHxJw8KuLt4sMrNq0poP7nmQlRkSj1iThWgf2XeYd9cZb6a";        

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < size; i++)

            sb.Append(legalChars.Substring(r.Next(0, legalChars.Length - 1), 1));

        return sb.ToString();

    }
    public static bool IsValidEmail(string email)
    {

        string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
          (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";

        Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);

        bool valid = false;


        if (string.IsNullOrEmpty(email))
        {
            valid = false;
        }
        else
        {

            valid = check.IsMatch(email);
        }

        return valid;
    }
  
    public static string GetTime()
    {
        string TimeInString = "";
        int hour = DateTime.Now.Hour;
        int min = DateTime.Now.Minute;
        int sec = DateTime.Now.Second;
        TimeInString += "[";
        TimeInString += DateTime.Now.ToShortDateString();
        TimeInString += ",";
        TimeInString += (hour < 10) ? "0" + hour.ToString() : hour.ToString();
        TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
        TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString());
        TimeInString += "]";
        return TimeInString;
    }
}

